#include <stdio.h>

#include <pthread.h>

#include <unistd.h>



//互斥锁

pthread_mutex_t g_lock;

//同步

pthread_cond_t thread_a_cond; 

pthread_cond_t thread_b_cond;



int g_is_my_turn = 0;



void* thread_a_start(void* arg){

  (void)arg;

  while(1){

    pthread_mutex_lock(&g_lock);

    while(g_is_my_turn == 1){

      pthread_cond_wait(&thread_a_cond, &g_lock);

    }

    printf("线程A打印-我是线程A\n");

    g_is_my_turn++;

    pthread_mutex_unlock(&g_lock);

    pthread_cond_signal(&thread_b_cond);

  }

}





void* thread_b_start(void* arg){

  (void)arg;

  while(1){

    sleep(1);

    pthread_mutex_lock(&g_lock);

    while(g_is_my_turn == 0){

      pthread_cond_wait(&thread_b_cond, &g_lock);

    }

    printf("线程B打印-我是线程B\n");

    g_is_my_turn--;

    pthread_mutex_unlock(&g_lock);

    pthread_cond_signal(&thread_a_cond);

  }

}





int main(){

  pthread_mutex_init(&g_lock, NULL);

  pthread_cond_init(&thread_a_cond, NULL);

  pthread_cond_init(&thread_b_cond, NULL);



  pthread_t thread_a, thread_b;

  int ret = pthread_create(&thread_a, NULL, thread_a_start, NULL);

  if(ret < 0){

    perror("pthread_create");

    return 0;

  }



  ret = pthread_create(&thread_b, NULL, thread_b_start, NULL);

  if(ret < 0){

    perror("pthread_create");

    return 0;

  }

   

  pthread_join(thread_a, NULL);

  pthread_join(thread_b, NULL);



  pthread_mutex_destroy(&g_lock);

  pthread_cond_destroy(&thread_a_cond);

  pthread_cond_destroy(&thread_b_cond);

  return 0;

}